package com.ubing.boot.notesforjava.response;

import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName : Response
 * @Description : 请求响应体封装类
 * @Author : ubing
 * @Date: 2023-02-15 19:14
 */
@Data
public class Response<T> {
    private static final long serialVersionUID = 1L;
    // 状态吗，0-失败，1-成功
    private Integer code;

    @Override
    public String toString() {
        return "Response{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    // 提示信息
    private String msg;
    // 返回数据
    private  T data;

    public Response code(Integer code){
        this.code=code;
        return this;
    }

    public Response msg(String msg){
        this.msg=msg;
        return this;
    }

    public Response data(T data){
        this.data=data;
        return this;
    }

    public static Response success(Object o){
        return new Response().code(CodeEnum.SUCCESS.getCode()).msg(CodeEnum.SUCCESS.getInfo()).data(o);
    }
    public static Response success(){
        return new Response().code(CodeEnum.SUCCESS.getCode()).msg(CodeEnum.SUCCESS.getInfo());
    }

    public static Response failure(){
        return new Response().code(CodeEnum.FAILURE.getCode()).msg(CodeEnum.FAILURE.getInfo());
    }

    public static Response failure(String msg){
        return new Response().code(CodeEnum.FAILURE.getCode()).msg(msg);
    }

    public static Response tokenError(Object o){return new Response().code(CodeEnum.TOKEN_ERROR.getCode()).msg(CodeEnum.TOKEN_ERROR.getInfo()).data(o);}

    public static Response tokenError(String msg){return new Response().code(CodeEnum.TOKEN_ERROR.getCode()).msg(msg);}


}
